// https://www.lintcode.com/problem/set-matrix-zeroes/description

class Solution {
public:
    /**
     * @param matrix: A lsit of lists of integers
     * @return: nothing
     */
    void setZeroes(vector<vector<int>> &matrix) {
        if (matrix.empty()) return;
        int m = matrix.size();
        int n = matrix[0].size();
        bool rowZero = false;
        bool colZero = false;
        //注意不能把第0行第0列填了，不然都变成0，先记一下
        for (int i = 0; i < m; ++i)
        {
            if (matrix[i][0] == 0) 
            {
                colZero = true;
                break;
            }
        }
        for (int j = 0; j < n; ++j)
        {
            if (matrix[0][j] == 0) 
            {
                rowZero = true;
                break;
            }
        }
        for (int i = 1; i < m; ++i)
        {
            for (int j = 1; j < n; ++j)
            {
                if (matrix[i][j] == 0)
                {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        for (int i = 1; i < m; ++i) 
        {
            if (matrix[i][0] == 0)
            {
                for (int j = 1; j < n; ++j)
                {
                    matrix[i][j] = 0;
                }
            }
        }
        for (int j = 1; j < n; ++j)
        {
            if (matrix[0][j] == 0)
            {
                for (int i = 1; i < m; ++i)
                {
                    matrix[i][j] = 0;
                }
            }
        }
        if (colZero)
        {
            for (int i = 0; i < m; ++i) matrix[i][0] = 0;
        }
        if (rowZero)
        {
            for (int j = 0; j < n; ++j) matrix[0][j] = 0;
        }
    }
};